﻿Imports System.Xml

Public Class Editor

    Public Const DocURL As String = "D:/Project SimpleCMS/Content/MainPageContent.xml"

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'UpdateXml(TextBox1.Text, Content.Constants.RightTopHeading)
        'Dim hindiEditor As New Hindi_Editor()
        'hindiEditor.Tag = "RightTopHeading"
        'hindiEditor.ShowDialog()
    End Sub

    Sub UpdateXml(ByVal value As String, ByVal node As String)
        Dim xmldoc As New XmlDocument()
        xmldoc.Load(DocURL)
        Dim xmlNode As XmlNode = xmldoc.SelectSingleNode(node)
        Try
            xmlNode.InnerText = value
        Catch e As Exception
            MessageBox.Show(e.Message)
        End Try
        xmldoc.Save(DocURL)
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'CreateHindiBox()
    End Sub

  

    Private Sub Editor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        InitializeUI()
        InitializeContent()
    End Sub


    Sub InitializeUI()
        Dim doc As mshtml.IHTMLDocument2
        WbRightTopContent.DocumentText = "<html><body></body></html>"
        doc = WbRightTopContent.Document.DomDocument
        doc.designMode = "Off"

        Dim doc1 As mshtml.IHTMLDocument2
        WbRightContentBottom.DocumentText = "<html><body></body></html>"
        doc1 = WbRightContentBottom.Document.DomDocument
        doc1.designMode = "Off"

        Dim doc2 As mshtml.IHTMLDocument2
        WbRightTopHeading.DocumentText = "<html><body></body></html>"
        doc2 = WbRightTopHeading.Document.DomDocument
        doc2.designMode = "Off"

        LanguageComboBox.Items.Add("English")
        LanguageComboBox.Items.Add("English/Hindi")
        LanguageComboBox.SelectedItem = "English/Hindi"

        Timer1.Interval = 500
        Timer1.Start()
        Timer1.Enabled = True
    End Sub

   
    Sub InitializeContent()
        Dim xmldoc As New XmlDocument()

        xmldoc.Load(DocURL)

        ' needs refactoring.
        WbRightTopContent.DocumentText = TranslateTextToSymbols(xmldoc.SelectSingleNode(Content.Constants.RightTopContent).InnerXml)
        WbRightTopHeading.DocumentText = xmldoc.SelectSingleNode(Content.Constants.RightTopHeading).InnerXml
        WbRightContentBottom.DocumentText = xmldoc.SelectSingleNode(Content.Constants.RightBottomContent).InnerXml
        txtMenu1.Text = xmldoc.SelectSingleNode(Content.Constants.MenuOne).InnerXml
        TextBox5.Text = xmldoc.SelectSingleNode(Content.Constants.MenuTwo).InnerXml
        TextBox6.Text = xmldoc.SelectSingleNode(Content.Constants.MenuThree).InnerXml
        TextBox7.Text = xmldoc.SelectSingleNode(Content.Constants.MenuFour).InnerXml
        TextBox8.Text = xmldoc.SelectSingleNode(Content.Constants.MenuFive).InnerXml


    End Sub




    Private Sub Button5_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
        OpenEditForm(txtMenu1)
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim hindiEditor As New Hindi_Editor()
        hindiEditor.ShowDialog()
    End Sub

    Public Sub OpenEditForm(ByRef cntl As Control)
        Dim edit = New Edit()
        edit.ShowDialog()
        If edit.DialogResult = Windows.Forms.DialogResult.OK Then
            cntl.Text = edit.txtText.Text
            UpdateXml(edit.txtText.Text, Content.Constants.MenuOne)
        End If
    End Sub

    Private Sub BtnMenu1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnMenu1.Click

    End Sub

    Private Sub Button19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button19.Click

    End Sub

    Private Sub Button20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button20.Click
        'Dim frm As New Design.EditorForm()
        'frm.ShowDialog()
        WbRightContentBottom.DocumentText = RunEditor(WbRightContentBottom.DocumentText)
    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        
        WbRightTopContent.DocumentText = RunEditor(WbRightTopContent.DocumentText)
    End Sub


    Private Function RunEditor(ByVal inputText As String) As String
        Dim EditedText As String = String.Empty
        Dim frm As Form = New Design.EditorForm(inputText)
        frm.ShowDialog()
        If frm.DialogResult = Windows.Forms.DialogResult.OK Then
            Return DirectCast(frm.Controls("editor").Controls("webBrowser1"), WebBrowser).DocumentText
        Else
            Return inputText
        End If
        Return EditedText
    End Function


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        FormatContent()
        Timer1.Stop()
    End Sub

    Sub FormatContent()
        WbRightTopContent.Document.ExecCommand("SelectAll", True, Nothing)
        WbRightTopContent.Document.ExecCommand("FontSize", True, "1")
        WbRightTopContent.Document.ExecCommand("Unselect", True, Nothing)

        WbRightContentBottom.Document.ExecCommand("SelectAll", True, Nothing)
        WbRightContentBottom.Document.ExecCommand("FontSize", True, "1")
        WbRightContentBottom.Document.ExecCommand("Unselect", True, Nothing)


        WbRightTopHeading.Document.ExecCommand("SelectAll", True, Nothing)
        WbRightTopHeading.Document.ExecCommand("FontSize", True, "1")
        WbRightTopHeading.Document.ExecCommand("Unselect", True, Nothing)

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        If WbRightTopContent.Document.Body.FirstChild IsNot Nothing Then
            UpdateXml(WbRightTopContent.Document.Body.InnerHtml(), Content.Constants.RightTopContent)
        Else
            UpdateXml(WbRightTopContent.Document.Body.InnerText(), Content.Constants.RightTopContent)
        End If

    End Sub

    Private Function TranslateTextToSymbols(ByVal text As String) As String
        text = text.Replace("&lt;", "<")
        text = text.Replace("&gt;", ">")
        text = text.Replace("&nbsp;", " ")
        Return text
    End Function

End Class


